package com.syj.jsxjj.ui.lend.adapter;


import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.syj.jsxjj.R;
import com.syj.jsxjj.config.Constant;
import com.syj.jsxjj.ui.lend.bean.LoanList;
import com.syj.jsxjj.widget.recycler.BaseRecyclerAdapter;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2016/11/17 0017.
 */

public class LoanListAdapter extends BaseRecyclerAdapter<LoanListAdapter.ViewHolder, LoanList.ListBean> {

    private Map<String, String> labelColMap;
    private Map<String, String> labelMap;

    public void setLabelColMap(Map<String, String> labelColMap) {
        this.labelColMap = labelColMap;
    }

    public void setLabelMap(Map<String, String> labelMap) {
        this.labelMap = labelMap;
    }

    @Override
    public LoanListAdapter.ViewHolder mOnCreateViewHolder(ViewGroup parent, int viewType) {
        return new LoanListAdapter.ViewHolder(mInflater.inflate( R.layout.list_item_loan, parent, false));
    }
    
    @Override
    public void mOnBindViewHolder(ViewHolder holder, int position) {
        Glide.with(mContext)
                .load(Constant.IMG_URL + item.getCompanyLogo())
                .error(R.mipmap.icon_default)
                .placeholder(R.mipmap.icon_default)
                .into(holder.mIvLogo);
        holder.mTvTitle.setText(item.getProductName());
        holder.mTvContent.setText("最高可借：" + item.getQutaRangeEnd() + "元");

        //替换上面的0,1,2
        holder.mTvRateTxt.setText("利率最低：");
        holder.mTvRate.setText(item.getRate() + "%");

//        SpannableString spanText = new SpannableString(item.getApplySuccessNum() + "人");
//        spanText.setSpan(new ForegroundColorSpan(Color.RED), 0, spanText.length() - 1,
//                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.mTvNumberOfPeople.setText(item.getApplySuccessNum());


        if (!TextUtils.isEmpty(item.getLabels()) && labelColMap != null && labelMap != null) {
            String[] lables = item.getLabels().split(",");
            if (lables.length == 1) {
                holder.mTvLabel1.setVisibility(View.VISIBLE);
                holder.mTvLabel2.setVisibility(View.GONE);
            } else if (lables.length >= 2) {
                holder.mTvLabel1.setVisibility(View.VISIBLE);
                holder.mTvLabel2.setVisibility(View.VISIBLE);
            } else {
                holder.mTvLabel1.setVisibility(View.GONE);
                holder.mTvLabel2.setVisibility(View.GONE);
            }

            for (int i = 0; i < lables.length; i++) {
                if (i > 1) break;
                if (i == 0) {
                    GradientDrawable drawable = (GradientDrawable) holder.mTvLabel1.getBackground();
                    drawable.setColor(Color.parseColor(labelColMap.get(lables[i])));

                    holder.mTvLabel1.setText(labelMap.get(lables[i]));
                    holder.mTvLabel1.setBackgroundDrawable(drawable);
                } else if (i == 1) {
                    GradientDrawable drawable = (GradientDrawable) holder.mTvLabel2.getBackground();
                    drawable.setColor(Color.parseColor(labelColMap.get(lables[i])));

                    holder.mTvLabel2.setText(labelMap.get(lables[i]));
                    holder.mTvLabel2.setBackgroundDrawable(drawable);
                }
            }
        } else {
            holder.mTvLabel1.setVisibility(View.GONE);
            holder.mTvLabel2.setVisibility(View.GONE);
        }

    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.iv_logo)
        ImageView mIvLogo;
        @BindView(R.id.view_line)
        View mViewLine;
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.tv_label_1)
        TextView mTvLabel1;
        @BindView(R.id.tv_label_2)
        TextView mTvLabel2;
        @BindView(R.id.tv_content)
        TextView mTvContent;
        @BindView(R.id.tv_rate_txt)
        TextView mTvRateTxt;
        @BindView(R.id.tv_rate)
        TextView mTvRate;
        @BindView(R.id.tv_number_of_people_txt)
        TextView mTvNumberOfPeopleTxt;
        @BindView(R.id.tv_number_of_people)
        TextView mTvNumberOfPeople;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
